package com.sebiotec.driver.adapt;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;

import com.chad.library.adapter.base.BaseDelegateMultiAdapter;
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sebiotec.basecommon.utils.entity.event.CountEvent;
import com.sebiotec.basecommon.utils.tools.TimeUtil;

import com.sebiotec.driver.R;

import com.sebiotec.driver.model.response.MessListResponse;
import com.sebiotec.driver.ui.activity.MessageDetailActivity;
import com.sebiotec.driver.viewmodel.MessageViewModel;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class MessageListAdapt extends BaseDelegateMultiAdapter<MessListResponse.RowsBean, BaseViewHolder> {

    private final MessageViewModel messageViewModel;

    public MessageListAdapt(FragmentActivity activity) {
            super();
         messageViewModel = ViewModelProviders.of(activity).get(MessageViewModel.class);
        setMultiTypeDelegate(new BaseMultiTypeDelegate<MessListResponse.RowsBean>() {
            @Override
            public int getItemType(@NonNull List<? extends MessListResponse.RowsBean> data, int position) {
                // 根据数据，自己判断应该返回的类型


                    switch (Integer.parseInt(data.get(position).getBKStatus())) {
                        case -1:
                            return 1;
                        case -2:
                            return 1;
                        case 0:
                            return 2;
                        case 1:
                            return 2;
                        case 2:
                            return 2;
                        case 3:
                            return 2;
                        case 4:
                            return 2;
                        case 5:
                            return 2;
                        case 6:
                            return 2;
                        case 7:
                            return 2;
                        case 8:
                            return 2;
                        case 9:
                            return 1;
                        default:
                            return  2;

                    }




            }
        });
        // 第二部，绑定 item 类型
        getMultiTypeDelegate()

                .addItemType(1, R.layout.book_iitem_message2)//红色
                .addItemType(2, R.layout.book_item_message3);//黑色

    }

    @Override
    protected void convert(BaseViewHolder helper,MessListResponse.RowsBean item) {

        SwipeMenuLayout viewById1 = helper.itemView.findViewById(R.id.sw);
        if (0==item.getReadStatus()){
            //没有读
            helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.VISIBLE);
        }else{
            //读了
            helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.GONE);
        }

        helper.itemView.findViewById(R.id.cl_ll).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                viewById1.smoothExpand();
                if (0==item.getReadStatus()){
                    //请求接口告诉后台消息已经查看
                    messageViewModel.isRead(item.getId() + "", 1, new MessageViewModel.MyHistoryDataCallBack() {
                        @Override
                        public void onDataSuccessCallBack(Object o) {
                            helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.GONE);
                            getData().get(helper.getLayoutPosition()).setReadStatus(1);
                             //发消息 告诉Tab数量减一
                            EventBus.getDefault().post(new CountEvent());
                        }

                        @Override
                        public void onDataFailCallBack(Object o) {
                            helper.itemView.findViewById(R.id.v_xhd).setVisibility(View.GONE);
                        }
                    });

                }


                return true;
            }
        });
        viewById1.setSwipeEnable(false);
        helper.itemView.findViewById(R.id.cl_ll).setOnClickListener(v->{
            if (viewById1.isExpanding()){
                viewById1.smoothClose();
            }

        });
        helper.itemView.findViewById(R.id.iv_delete).setOnClickListener(v->{
            if (viewById1.isExpanding()){
                viewById1.smoothClose();
            }
//                  if (Integer.parseInt(item.getBKStatus())!=-2&&Integer.parseInt(item.getBKStatus())!=9&&Integer.parseInt(item.getBKStatus())!=-1){
//                    Intent   intent =new Intent(getContext(), MessageDetailActivity.class);
//                   intent.putExtra("BookingId",item.getBookingId());
//                  getContext().startActivity(intent);
//                    }

            if (Integer.parseInt(item.getBKStatus())!=-2&&Integer.parseInt(item.getBKStatus())!=-1){
                Intent   intent =new Intent(getContext(), MessageDetailActivity.class);
                intent.putExtra("BookingId",item.getBookingId());
                getContext().startActivity(intent);
            }

//
//                Intent   intent =new Intent(getContext(), MessageDetailActivity.class);
//                intent.putExtra("BookingId",item.getBookingId());
//                getContext().startActivity(intent);


        });
        switch (Integer.parseInt(item.getBKStatus())) {
            //1:Ambassador pickup 2:Driver pickup 3: Completed(Driver pickdown) 4:Rejected
            case 1:
                helper.setText(R.id.tv_upcoming_name,getContext().getString(R.string.adapt_confirmed_bys));
                break;
            case 2:
                helper.setText(R.id.tv_upcoming_name, getContext().getString(R.string.greeted_bys));
                break;
            case 3:
                helper.setText(R.id.tv_upcoming_name, getContext().getString(R.string.Picked_up_bys));
                break;
            case 4:
                helper.setText(R.id.tv_upcoming_name, getContext().getString(R.string.droped_off_bys));
                break;
            case 5:
                helper.setText(R.id.tv_upcoming_name, getContext().getString(R.string.completed_bys));
                break;
            case -1:
                helper.setText(R.id.tv_upcoming_name,getContext().getString(R.string.is_rejected));
                break;
            case 7:
                helper.setText(R.id.tv_upcoming_name, getContext().getString(R.string.dispatched_bys));
                break;
            case 13:
                helper.setText(R.id.tv_upcoming_name,getContext().getString(R.string.updated) );
                break;
            case 0:
                helper.setText(R.id.tv_upcoming_name, getContext().getString(R.string.created_bys));
                break;
            case -2:
                helper.setText(R.id.tv_upcoming_name,getContext().getString(R.string.cancells));

                break;
            case 9:
                helper.setText(R.id.tv_upcoming_name,getContext().getString(R.string.no_show));
                break;
            default:
                helper.itemView.findViewById(R.id.ll_dx).setVisibility(View.GONE);
                break;
        }

        helper.setText(R.id.tv_num,  TimeUtil.getFormatData_O(item.getCreateDate()));

        helper.setText(R.id.tv_address, item.getPickupFrom());
        helper.setText(R.id.tv_pickup_time,item.getPickupDateTime());
        helper.setText(R.id.tv_to_address, item.getDestination());
        helper.setText(R.id.tv_titles,item.getName());


    }
}